// pages/myself/commission/CustomerService.js
var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
var start = 0;
const app = getApp();

Page(Object.assign({}, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    refundOrder: [],
    recordsTotal: 0,
    nodata: {
      msg: '无相关数据',
      notype: 1,
      imgurl: '/images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    network._post_page_json(this, api.getRefundOrderList, start, {}, success);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      refundOrder: [],
    });
    network._post_page_json(this, api.getRefundOrderList, start, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.refundOrder.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.getRefundOrderList, start, {}, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  refundorderdetails: function (e) {
    var refundorderid = e.currentTarget.dataset.refundorderid;
    if (refundorderid) {
      network._post_page_json(this, api.getRefundOrderDetail, start, { id: refundorderid }, Rsuccess);
    }
  }
}))

var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      refundOrder: res.data.data
    })
  }
}

var Rsuccess = function (that, res) {
  var callbackdata = res.data.data;
  var product = callbackdata.product;
  if (product.productSpec) {
    var productprice = product.productSpec.price;
    var pspec = product.productSpec.name + (product.productSpec.name1 ? ('+' + product.productSpec.name1) : '') + (product.productSpec.name2 ? ('+' + product.productSpec.name2) : '');
  } else {
    var productprice = product.price;
  }
  if (product.applicationBack.type == 1) {
    var ordertype = '已经发货'
  } else {
    var ordertype = '未发货'
  }
  if (callbackdata.status == 0) {
    var Refundresults = '同意退款'
  } else if (callbackdata.status == 1) {
    var Refundresults = '拒绝售后，请联系商家'
  } else if (callbackdata.status == 2) {
    var Refundresults = '退款成功'
  } else if (callbackdata.status == 4) {
    var Refundresults = '正在处理售后，请等待'
  }
  var createDate = utils.formatDate(new Date(callbackdata.createDate));
  if (callbackdata.endDate != null) {
    var endDate = utils.formatDate(new Date(callbackdata.endDate));
  } else {
    var endDate = callbackdata.endDate;
  }
  if (product.proofRefunds.length != 0) {
    var tuihuopingz = product.proofRefunds;
  } else {
    var tuihuopingz = null;
  }
  var getdata = {
    'productimg': product.smallImgUrl,
    'producttitle': product.title,
    'productspec': pspec,
    'productprice': productprice,
    'productcount': product.count,
    'agreeafterprice': callbackdata.refund_fee,
    'tuikuan': ordertype,
    'tuikuanwhy': product.applicationBack.reason,
    'tuikuancontent': product.applicationBack.refund_content,
    'tuikuanresults': Refundresults,
    'istongyi': callbackdata.status,
    'isfahuo': product.applicationBack.type,
    'tuihuoorderid': callbackdata.id,
    'expressName': callbackdata.expressName,
    'expressNo': callbackdata.expressNo,
    'createdate': createDate,
    'enddate': endDate,
    'out_refund_no': callbackdata.out_refund_no,
    'ordertotal': callbackdata.oldTotal,
    'maijialiu': callbackdata.content,
    'ordertype': product.applicationBack.type,
    'tyueyouhui': productprice * product.count - callbackdata.refund_fee,
    'tuihuopingz': tuihuopingz,
    'ispingtuan': product.applicationBack.reason,
    'linkType': product.applicationBack.linkType
  }
  wx.setStorageSync('neworder', getdata);
  if (callbackdata.status == 0 || callbackdata.status == 4 || callbackdata.status == 1) {
    wx.navigateTo({
      url: '/pages/myself/order/shouhou/waitingSale',
    })
  } else if (callbackdata.status == 2) {
    wx.navigateTo({
      url: '/pages/myself/order/shouhou/refunded',
    })
  }
}